package mydefaultpkg;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; 
import org.w3c.dom.*;

import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;


public class XMLHandlerImpl{

	String errorMSG = null ;
	private String resourcePath ="C:\\xml\\";
	private int counter =0;
	
	public boolean validateXML(String lFileName)
	{
		String filePath = resourcePath+lFileName;
		
		try{
			File lFile = new File(filePath);
			if(!lFile.exists()){
				errorMSG = "File not found " + filePath;
				return false;
			}
	
			XMLReader reader = XMLReaderFactory.createXMLReader();
			reader.parse(filePath);
		}
		catch(SAXParseException spe){
			//errorMSG = "Parser Exception while parsing file "+filePath;
			errorMSG = "Parser Exception while parsing file "+filePath +"  at line " +spe.getLineNumber()+":"+ spe.getColumnNumber() ;
			return false;
		}
		catch(SAXException se){	
			errorMSG = "Parser Exception while parsing file : "+se.getMessage();
			return false;
		}
		catch(IOException ioe)
		{
			errorMSG = "IO Exception while parsing file "+filePath;
			return false;
		}

		return true;
	}
	
	public void getXMLData(String lFileName)

	{
		
		String filePath = resourcePath+lFileName;
		try{
			
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser  parser		 = spf.newSAXParser();
			
			DefaultHandler dh = new DefaultHandler()
			{
				boolean company_details,name,email,phone;
				
				public void startElement(String uri, String localName, 
				           String element_name, Attributes attributes)throws SAXException{
					if(element_name.equals("name"))
						counter++;
						
					System.out.println("******************");
					

					for(int idx=0;idx<attributes.getLength();idx++)
					{
						System.out.println("attrib type "+attributes.getType(idx));
						System.out.println("attrib localname "+attributes.getLocalName(idx));
						System.out.println("attrib value "+attributes.getValue(idx));
						//System.out.println("attrib "+attributes.getValue(idx));
					}
					if (element_name.equals("company-details")){
						company_details = true;
				    }else if(element_name.equals("name")){
		              name = true;
		            }else if(element_name.equals("email")){
		            	email = true;
		            }else if (element_name.equals("phone")){
		            	phone = true;
		            }
		         }

				public void characters(char[] ch, int start, int len) throws SAXException{
	            String str = new String (ch, start, len);
	            if (company_details){
	              System.out.println("company_details: "+str);
	              company_details = false;
	            }
	            if (name){
	              System.out.println("Name:   "+str);
	              name = false;
	            }
	            if (email){
	              System.out.println("email: "+str);
	              email = false;
	            }
	            if (phone){
		              System.out.println("phone: "+str);
		              phone = false;
		            }
	          }
				
			};
			parser.parse(filePath,dh);
			System.out.println("counter value "+ counter);
			
		}
		catch(Exception e)
		{
			e.getMessage();
		}
	}
	
	public void getXMLData(String lFileName,String parseType)
	{
		
	}
	public void createDocument(String lFileName)
	{
		String filePath = resourcePath+lFileName;
		try
		{
			DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder		builder 	 = dbfactory.newDocumentBuilder();
			//Document			document	 = builder.newDocument();
			Document			document	 = builder.parse(filePath);
			
			Node rootNode = document.getDocumentElement(); 
			
			//Comment  comment = document.createComment("comment about company details");
			//rootNode.appendChild(comment);
			
			Element  company_details = document.createElement("company-details");
			rootNode.appendChild(company_details);
			
			//company_details.appendChild(comment);
			company_details.setAttribute("comtype", "public");
			company_details.setAttribute("ltd", "true");
			
			Element name = document.createElement("name");
			name.setTextContent("Abhi Corp");
			
			Element email = document.createElement("email");
			email.setTextContent("a@a.com");
			
			Element phone = document.createElement("phone");
			phone.setTextContent("99090900");
			company_details.appendChild(name);
			company_details.appendChild(email);
			company_details.appendChild(phone);
			
			TransformerFactory tranFactory = TransformerFactory.newInstance(); 
		    Transformer transformer = tranFactory.newTransformer(); 
		    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		    
		    FileOutputStream fos = new FileOutputStream(filePath);
		    
		    Source src = new DOMSource(document);
		    Result dest = new StreamResult(fos);
		    
		    fos.flush();
		   // Result dest = new StreamResult(System.out); 
		    transformer.transform(src, dest); 
		}
		catch(Exception e)
		{System.out.println(e.getMessage());}
	}
}